<template>
  <div class="dashboard-editor-container" v-if="isAdmin">
    <panel-group/>

    <el-row style="background: #fff; padding: 16px 16px 0; margin-bottom: 32px">
      <line-chart :chart-data="lineChartData"/>
    </el-row>

    <el-row :gutter="24">
      <!--       <el-col :xs="24" :sm="24" :lg="8">
              <div class="chart-wrapper">
                <raddar-chart />
              </div>
            </el-col>-->
      <el-col :span="10">
        <div class="chart-wrapper">
          <bar-chart/>
        </div>
      </el-col>
      <el-col :span="14">
        <div class="chart-wrapper">
          <pie-chart
            :deviceCountData="pieChartData.deviceCountData"
            :pie-chart-data="pieChartData"
          />
        </div>
      </el-col>

    </el-row>
  </div>
  <div v-else>
    <el-card class="box-card" style="background: #fffffc;">
      <p style="margin: 0">
        <a>
      <h2 style="text-align: center;margin-top: 30px;font-weight: bold;font-size: 30px;">通知公告</h2>
      <el-table :data="newsList" v-loading="loading" @row-click="newsDetails">
        <el-table-column label="通知公告" prop="title">
          <template slot-scope="scope">
            <span>{{ scope.row.noticeTitle }}</span>
            <!--<span style="text-align: right">{{scope.row.createTime}}</span>-->
          </template>
        </el-table-column>
        <!-- <el-table-column label="创建时间" prop="createTime" sortable/>-->
        <el-table-column prop="createTime" width="200px"/>
      </el-table>
      <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getNewsList"/>
      </a>
      </p>
    </el-card>
  </div>
</template>

<script>
import PanelGroup from "./dashboard/PanelGroup";
import LineChart from "./dashboard/LineChart";
//import RaddarChart from "./dashboard/RaddarChart";
import PieChart from "./dashboard/PieChart";
import BarChart from "./dashboard/BarChart";
import {getFlightMonthCount, getTicketState} from "@/api/index";
import { listNotice} from "@/api/system/notice";

import Cookies from 'js-cookie'

const lineChartData = {
  newVisitis: {
    expectedData: [],
    dayData: [],
  },
};
const pieChartData = {
  deviceCountData: [],
  projectNameData: [],
};

export default {
  name: "Index",
  components: {
    PanelGroup,
    LineChart,
    //RaddarChart,
    PieChart,
    BarChart,
  },
  data() {
    return {
      lineChartData: lineChartData.newVisitis,
      pieChartData: pieChartData,
      isAdmin: true,
      newsList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total:0,
      loading: true,
    };
  },
  created() {
    if (Cookies.get("username") == "admin") {
    getFlightMonthCount().then(res => {
      let arr = [];
      let arr2 = [];
      res.data.forEach((item) => {
        arr.push(item.count);
        arr2.push(item.month);
      });
      lineChartData.newVisitis.expectedData = arr;
      lineChartData.newVisitis.dayData = arr2;
    })
    this.selectCountGroupByProject();
    }else {
      this.isAdmin = false
      this.getNewsList();
    }
  },
  methods: {
    selectCountGroupByProject() {
      getTicketState().then((res) => {
        this.$set(this.pieChartData, "deviceCountData", res.data);
        pieChartData.deviceCountData = res.data;
        res.data.forEach((item) => {
          pieChartData.projectNameData.push(item.name);
        });
        console.log(pieChartData.deviceCountData);
        console.log(pieChartData.projectNameData);
      });
    },
    // 获取新闻列表
    getNewsList() {
      this.loading = true;
      listNotice(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.newsList = res.rows;
          this.loading = false;
          this.total = res.total;
          //this.sortKey(this.newsList, "createTime");
        }
      });
    },
    //跳转到新闻详情
    newsDetails(row) {
      console.log(row)
      this.$router.push({
        path: "/noticedetails",
        query: { noticeId:row.noticeId},
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
